﻿/* 
 * Since 2008 - 2012
 */

package com.hh.bss.project.dao.impl;

import java.util.*;

import com.hh.bss.base.*;
import com.hh.bss.common.MapObjectRowMapper;
import com.hh.bss.common.page.*;
import com.hh.bss.common.page.impl.*;
import com.hh.bss.project.dao.*;
import com.hh.bss.project.entity.*;
import com.hh.bss.project.service.*;
import com.hh.bss.project.vo.query.*;
import com.hh.bss.util.*;





import static com.hh.bss.util.ObjectUtils.*;

import org.springframework.stereotype.Repository;

@Repository
public class SearchProjectCvCrmDaoImpl extends BaseHibernateDao<SearchProjectCvCrm,java.lang.Integer>  implements SearchProjectCvCrmDao<SearchProjectCvCrm,java.lang.Integer>{

	public Class getEntityClass() {
		return SearchProjectCvCrm.class;
	}
	
	public Page findPage(SearchProjectCvCrmQuery query) {
        //XsqlBuilder syntax,please see http://code.google.com/p/rapid-xsqlbuilder
        // [column]为字符串拼接, {column}为使用占位符. [column]为使用字符串拼接,如username='[username]',偷懒时可以使用字符串拼接 
        // [column] 为PageRequest的属性
        
		
		    Class[] os={SearchProjectCvCrmQuery.class};
    		Class property= org.springframework.beans.BeanUtils.findPropertyType(query.getField_type(), os);
        if (property!=null){
        	 if ("java.lang.String".equals(property.getName())){
	    			
	    			com.hh.bss.util.BeanUtils.setProperty(query, query.getField_type(),query.getQuery());
	    			
	    		}
	    		else if ("java.lang.Integer".equals(property.getName())){
	    			
	    			com.hh.bss.util.BeanUtils.setProperty(query, query.getField_type(), StringHelper.parseInteger(query.getQuery(), 0));
	    			
	    		}
	    		else if ("java.lang.Double".equals(property.getName())){
	    			com.hh.bss.util.BeanUtils.setProperty(query, query.getField_type(), StringHelper.parseDouble(query.getQuery(), 0));
	    		}
	    		else if ("java.util.Date".equals(property.getName())){
	    			com.hh.bss.util.BeanUtils.setProperty(query, query.getField_type(), DateHelper.toDate(query.getQuery(), DateHelper.DEFAULT_DATE_FORMAT));
	    		}
        }	 
        //生成sql2的原因是为了不喜欢使用xsqlbuilder的同学，请修改生成器模板，删除本段的生成
        StringBuffer sql2 = new StringBuffer();
        List values = new ArrayList();
        sql2.append("select * from ( ");
        sql2.append(" select a.id,a.project_id projectId,a.cv_id cvId,a.fit_job fitJob,a.cv_flags cvFlags,b.cv_name cvName,a.plan_dt planDt,a.crm_focus crmFocus,a.crm_type crmType,a.crm_dt crmDt,a.minutes,a.task_status taskStatus,a.crm_result crmResult,a.comment,a.creator,a.create_dt createDt,a.modifier,a.modified_dt modifiedDt from search_project_cv_crm a,cv b where a.cv_id=b.id ");
        sql2.append(" ) t where 1=1 ");
          if(isNotEmpty(query.getId())) {
            sql2.append(" and  t.id = ? ");
            values.add(query.getId());
        }
//          if(isNotEmpty(query.getProjectId())) {
//            sql2.append(" and  t.projectId = ? ");
//            values.add(query.getProjectId());
//        }
        if(isNotEmpty(query.getCvId())) {
            sql2.append(" and  t.cvId = ? ");
            values.add(query.getCvId());
        }
        if(isNotEmpty(query.getJobId())) {
            sql2.append(" and  exists(select 1 from contract_job_search_project a1,contract_job a2 where a2.id=? and a2.id=a1.job_id and a1.project_id=t.projectId ) ");
            values.add(query.getJobId());
        }  
       	if(isNotEmpty(query.getCrmType())){
       	 sql2.append(" and  t.crmType = ? ");
         values.add(query.getCrmType());
       	}
       	if(isNotEmpty(query.getCrmDtBegin())){
       		sql2.append(" and  t.crmDt >= ? ");
       		values.add(query.getCrmDtBegin());
       	}
     	if(isNotEmpty(query.getCrmDtEnd())){
       		sql2.append(" and  t.crmDt <= ? ");
       		//values.add(query.getCrmDtEnd());
       		values.add(DateHelper.add(query.getCrmDtEnd(), DateHelper.DAY, 1));
       	}
        if(isNotEmpty(query.getComment())){
        	sql2.append(" and  t.comment like ? ");
        	values.add("%"+query.getComment()+"%");
        }
        return super.getJdbcDao().createPage(sql2.toString(),values.toArray(), new MapObjectRowMapper(), query.getPageSize(), query.getPageNumber());
	}
	

}
